#pragma once
#include <iostream>
#include <memory>

#include "Protocal.hpp"

class Net_Cal
{
public:
    std::shared_ptr<Response> Calculator(std::shared_ptr<Request> &req)// 用于Service层回调的
    {
        std::shared_ptr<Response> resp = Factory::BuildResponse();
        switch (req->GetOper())
        {
            case '+':
                resp->SetResult(req->GetX() + req->GetY());
                break;
            case '-':
                resp->SetResult(req->GetX() - req->GetY());
                break;
            case '*':
                resp->SetResult(req->GetX() * req->GetY());
                break;
            case '/':
            {
                if (req->GetY() == 0)
                {
                    resp->SetCode(1);
                    resp->SetDesc("div zero");
                }
                else
                {
                    resp->SetResult(req->GetX() / req->GetY());
                }
            }
            break;
            case '%':
            {
                if (req->GetY() == 0)
                {
                    resp->SetCode(2);
                    resp->SetDesc("mod zero");
                }
                else
                {
                    resp->SetResult(req->GetX() % req->GetY());
                }
            }
            break;
            default :
            {
                resp->SetCode(3);
                resp->SetDesc("illegal operation");
            }
            break;
        }
        return resp;
    }
};
